<template>
	<view >
		<!-- 购物列表 -->
		<view class="goods-list">
			<view class="tis" v-show="goodsList.length == 0">购物车空空如也</view>
			<view class="row" v-for="(item, index) in goodsList" :key="index">
				<!-- 删除按钮 -->
				<view class="menu" @tap="handleSingleDelete(item)">
					<view class="icon iconfont">&#xe6a6;</view>
				</view>
				
				<!-- 商品 -->
				<view class="carrier" 
					@touchstart="handleTouchStart(index, $event)"
					@touchmove="handleTouchMove(index, $event)"
					@touchend="handleTouchEnd(index, $event)"
					:class="[theIndex == index ? 'open' : oldIndex == index ? 'close' : '']">
					<!-- checkbox -->
					<view class="checkbox-box" @tap="handleCheckbox(item)">
						<view class="checkbox">
							<view :class="{'on': item.selected}"></view>
						</view>
					</view>
					
					<!-- 商品详情 -->
					<view class="goods-info" @tap="handleGoodsInfo(item)">
						<view class="img">
							<image :src="item.img"></image>
						</view>
						<view class="info">
							<view class="title">{{item.name}}</view>
							<view class="spec">{{item.spec}}</view>
							<view class="price-number">
								<view class="price">￥{{item.price}}</view>
								<counter :goodsInfo="item" @change="sum"></counter>
							</view>
						</view>
					</view>
				
				</view>
			</view>
		</view>
		
		<!-- 底部菜单 -->
		<view class="footer" :style="{bottom: footerbottom}">
			<view class="checkbox-box" @tap="handleSelectedAll">
				<view class="checkbox">
					<view :class="{'on': isAllSelected}"></view>
				</view>
				<view class="text">全选</view>
			</view>
			<view class="delBtn" @tap="handleMulDelete" v-if="selectedList.length > 0">删除</view>
			<view class="settlement">
				<view class="sum">合计:<view class="money">￥{{sumPrice}}</view></view>
				<view class="btn">结算{{selectedList.length}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	import counter from '../../../components/counter'
		
	export default {
		data() {
			return {
				theIndex: null,	//	控制滑动效果，当前滑动下标
				oldIndex: null,	//	上一个左滑下标
				goodsList: [],
				selectedList: [],	//	选中的商品列表
				isAllSelected: false,
				sumPrice: '0.00',
				footerbottom: 0
			}
		},
		components: {
			counter
		},
		onLoad() {
			// 兼容h5的底部菜单
			// #ifdef H5
			this.footerbottom = document.getElementsByTagName("uni-tabbar")[0].offsetHeight + 'px'
			// #endif
		},
		onShow() {
			uni.getStorage({
				key: "goodsList",
				success: res => {
					// 将所有商品的选中状态都设置false
					for (let i=0; i<res.data.length; i++) {
						res.data[i].selected = false
					}
					
					this.goodsList = res.data
					
					// 属性数据的初始化
					this.selectedList = []
					this.isAllSelected = false
					this.sumPrice = '0.00'
				}
			})
		},
		methods: {
			handleCheckbox(item) {
				item.selected = !item.selected
				
				// 条件：数组中是否包含该元素
				let isExist = this.selectedList.indexOf(item)
				if (isExist > -1) {
					this.selectedList.splice(isExist, 1)
				} else {
					this.selectedList.push(item)
				}
				
				// 全选状态
				if (this.selectedList.length == this.goodsList.length) {
					this.isAllSelected = true
				} else {
					this.isAllSelected = false
				}
				
				this.sum()
			},
			handleGoodsInfo(item) {
				uni.navigateTo({
					url: "../../goods/goods?goodsInfo=" + JSON.stringify(item)
				})
			},
			handleTouchStart(index, event) {
				// 多点触控，不触发
				if (event.touches.length > 1) {
					return
				}
				
				this.oldIndex = this.theIndex
				this.theIndex = null
				
				// 初始化坐标
				this.initXY = [event.touches[0].pageX, event.touches[0].pageY]
			},
			handleTouchMove(index, event) {
				// 多点触控，不触发
				if (event.touches.length > 1) {
					return
				}
				
				// 移动位置
				let moveX = event.touches[0].pageX - this.initXY[0]
				let moveY = event.touches[0].pageY - this.initXY[1]
			
				// 滑动位置小，不触发
				if (Math.abs(moveX) < 5) {
					return
				}

				// 竖向滑动，不触发
				if (Math.abs(moveY) > Math.abs(moveX)) {
					return
				}

				// 左滑
				if (moveX < 0) {
					this.theIndex = index
				} else {
					// this.oldIndex = index
					this.oldIndex = this.theIndex
					this.theIndex = null
				}
			},
			handleTouchEnd(index, event) {
				// 多点触控，不触发
				if (event.touches.length > 1) {
					return
				}
			},
			handleSelectedAll() {
				this.isAllSelected = !this.isAllSelected
				
				// 数据处理
				let arr = [];
				this.goodsList.forEach((item, i) => {
					item.selected = this.isAllSelected
					arr.push(item)
				})
				
				this.selectedList = this.isAllSelected ? arr : []
			
				this.sum()
			},
			sum() {
				this.sumPrice = 0;
				this.goodsList.forEach((item, i) => {
					if (item.selected) {
						this.sumPrice = this.sumPrice + (item.number * item.price)
					}
				})
				
				this.sumPrice = this.sumPrice.toFixed(2)
			},
			handleSingleDelete(item) {
				// 更新storage
				uni.getStorage({
					key: "goodsList",
					success: res => {
						res.data.splice(res.data.indexOf(item), 1)
						uni.setStorageSync("goodsList", res.data)
					}
				})
				
				// 更新数组
				this.goodsList.splice(this.goodsList.indexOf(item), 1)
				this.selectedList.splice(this.selectedList.indexOf(item), 1)
				
				this.oldIndex = null
				this.theIndex = null
				
				this.sum()
			},
			handleMulDelete() {
				// 循环删除所有选中的商品
				while(this.selectedList.length > 0) {
					this.handleSingleDelete(this.selectedList[0])
				}
				
				// 属性数据的初始化
				this.selectedList = []
				this.isAllSelected = false
				this.sumPrice = '0.00'
			}
		}
	}
</script>

<style lang='scss'>
.checkbox-box{
	display: flex;
	align-items: center;
	
	.checkbox{
		width: 34rpx;
		height: 34rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 100%;
		border: solid 2rpx #f06c7a;

		
		.on{
			width: 24rpx;
			height: 24rpx;
			border-radius: 100%;
			background-color: #f06c7a;
		}
	}
	.text{
		margin-left: 10rpx;
		font-size: 28rpx;
	}
}
	
.goods-list{
	width: 100%;
	padding: 20rpx 0 120rpx 0;
	
	.tis{
		width: 100%;
		height: 60rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 32rpx;
		color: #a7a7a7;
	}
	.row{
		width: calc(92%);
		height: calc(22vw + 40rpx); 
		margin: 20rpx auto;
		
		border-radius: 15rpx;
		box-shadow: 0rpx 5rpx 20rpx rgba(0,0,0,0.1);
		display: flex;
		align-items: center;
		position: relative;
		z-index: 4;
		border: 0;
		overflow: hidden;
		.menu{
			.icon{
				color: #fff;
				font-size: 60rpx;
			}		
			width: 30%;
			height: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			position: absolute;
			right: 0;
			background-color: red;
			color: #fff;
			z-index: 2;
		}
		
		.carrier{
			@keyframes showMenu {
				0% {transform: translateX(0);}
				100% {transform: translateX(-30%);}
			}
			@keyframes closeMenu {
				0% {transform: translateX(-30%);}
				100% {transform: translateX(0);}
			}
			&.open{
				animation: showMenu 0.25s linear both;
			}
			&.close{
				animation: closeMenu 0.15s linear both;
			}
			
			width: 100%;
			height: 100%;
			padding: 0 0;
			display: flex;
			align-items: center;
			position: absolute;
			background-color: #fff;
			z-index: 3;


			.checkbox-box{
				height: 22vw;
				padding-left: 20rpx;
				margin-right: 20rpx;
				flex-shrink: 0;
			}

			.goods-info{
				width: 100%;
				padding-right: 20rpx;
				display: flex;
				.img{
					width: 22vw;
					height: 22vw;
					margin-right: 10rpx;
					flex-shrink: 0;
					border-radius: 10rpx;
					overflow: hidden;
					image{
						width: 22vw;
						height: 22vw;
					}
				}
				.info{
					width: 100%;
					height: 22vw;
					display: flex;
					flex-wrap: wrap;
					position: relative;
					overflow: hidden;
					
					.title{
						width: 100%;
						font-size: 28rpx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						/* text-align: justify; */
						overflow: hidden;
					}
					
					.spec{
						height: 30rpx;
						padding: 0 10rpx;
						margin-bottom: 20vw;
						display: flex;
						align-items: center;
						border-radius: 15rpx;
						font-size: 20rpx;
						background-color: #f3f3f3;
						color: #a7a7a7;					
					}
					.price-number{
						width: 100%;
						height: 60rpx;
						display: flex;
						justify-content: space-between;
						align-items: flex-end;
						position: absolute;
						bottom: 0rpx;						
						font-size: 28rpx;
						.price{
							
						}
					}
				}
			}
		}
	}
}

.footer{
	width: 92%;
	padding: 0 4%;
	background-color: #fbfbfb;
	height: 100rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	font-size: 28rpx;
	position: fixed;
	bottom: 0rpx;
	z-index: 5;
	.delBtn{
		border: solid 1rpx #f06c7a;
		color: #f06c7a;
		padding: 0 30rpx;
		height: 50rpx;
		border-radius: 30rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.settlement{
		width: 60%;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		.sum{
			width: 50%;
			font-size: 28rpx;
			margin-right: 10rpx;
			display: flex;
			justify-content: flex-end;
			.money{
				font-weight: 600;
			}
		}
		.btn{
			padding: 0 30rpx;
			height: 50rpx;
			background-color: #f06c7a;
			color: #fff;
			display: flex;
			justify-content: center;
			align-items: center;
			
			border-radius: 30rpx;
		}
	}
}
</style>
